package com.wisdomparking.control.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.wisdomparking.bean.Manager;
import com.wisdomparking.bean.Orders;
import com.wisdomparking.bean.User;
import com.wisdomparking.config.result.TempResult;
import com.wisdomparking.constant.Constants;
import com.wisdomparking.control.BaseController;
import com.wisdomparking.service.OrdersService;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.UserService;
import com.wisdomparking.service.VehicleInfoService;
import com.wisdomparking.tools.PageForAjax;
import com.wisdomparking.tools.ToolValid;

/**
 * 订单中心
 */
@RestController
@RequestMapping("/web/orderCenter")
public class OrdersController extends BaseController{
	
	@Autowired OrdersService ordersService;
	@Autowired VehicleInfoService vehicleInfoService;
	@Autowired ParkingService parkingService;
	@Autowired UserService userService;
	
	
	/**
     * 首页
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
    	
        return new ModelAndView("orders/index");
    }
	
	/**
     * 左侧
     *
     * @return
     */
    @RequestMapping(value = "/left")
    public ModelAndView left(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("orders/left");
    }
    
    /**
     * 转到订单信息处理
     *
     * @return
     */
    @RequestMapping(value = "/handlePage")
    public ModelAndView handlePage() {
    	
    	if(ToolValid.isNotEmpty(request.getParameter("id"))){
    		Orders orders = ordersService.getOrders(Integer.parseInt(request.getParameter("id")));
    		
    		request.setAttribute("orders", orders);
    	}
    	
        return new ModelAndView("orders/op");
    }
    
    
    /**
	 * 获取订单信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getOrders")
	public ModelAndView getOrders(Integer id){
		Orders orders = ordersService.getOrders(id);
		
		if(ToolValid.isNotNull(orders)){
			User user = userService.getUser(orders.getUserId());
			if(ToolValid.isNotNull(user)){
				request.setAttribute("user", user);
			}
		}
		
		request.setAttribute("orders", orders);
		
		return new ModelAndView("orders/op");
	}
	
	
	/**
	 * 删除订单
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delOrders")
	public TempResult delOrders(Integer id){
		boolean flag = false;
		
		Orders orders = ordersService.getOrders(id);
		orders.setDelFlag(true);
		flag = ordersService.updateOrders(orders, false);
		
		return new TempResult(flag, "");
	}
	
	/**
	 * 获取车场订单
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping("/getOrdersList")
	public ModelAndView getOrderList(HttpServletRequest request){
		Integer parkingId = 0;
		Manager loginManger = (Manager)request.getSession().getAttribute(Constants.LOGININFO);
		if(ToolValid.isNotNull(loginManger)){
			switch (loginManger.getManagerGrade()) {
			case 0:
				parkingId = 0;
				break;
			case 1:
				parkingId = loginManger.getId();
				break;
			case 2:
				parkingId = loginManger.getManagerId();
				break;

			default:
				break;
			}
		}
		
		String url = request.getContextPath() + "/web/orderCenter/getOrdersList";
		String query = "";
		PageForAjax pagecontent = null;
		
		int pageNo = 1;
		int pageSize = 10;
		int totalcount = 0;
		
		if(null!=request.getParameter("pageNo")){
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		
		if(null!=request.getParameter("pageSize")){
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("start", (pageNo-1)*pageSize);
		map.put("limit", pageSize);
		map.put("delFlag", 0);
		
		if(parkingId!=0){
			map.put("parkingId", parkingId);
		}
		
		if(ToolValid.isNotEmpty(request.getParameter("key"))){
			map.put("orderNumber", request.getParameter("key"));
			request.setAttribute("key", request.getParameter("key"));
		}
		
		List<Orders> list = ordersService.getOrdersList(map);
		totalcount = new Long(ordersService.getCountByOrders(map)).intValue();

		pagecontent = new PageForAjax(pageNo, pageSize, totalcount, url, query);

		request.setAttribute("ordersList", list);
		request.setAttribute("nowpage", pagecontent);
		
		
		
		return new ModelAndView("orders/orders");
	}
	
}
